package com.sumainfor.zuul.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.sumainfor.common.annotation.SysLog;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.zuul.entity.SysAreaEntity;
import com.sumainfor.zuul.entity.SysDeptEntity;
import com.sumainfor.zuul.service.SysAreaDeptService;
import com.sumainfor.zuul.service.SysAreaService;
import com.sumainfor.zuul.service.SysDeptService;
import com.sumainfor.zuul.service.impl.AreaService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author PuaChen
 * @Create 2018-12-29 16:44
 */
@RestController
@RequestMapping("sys/area")
public class SysAreaController {

    @Autowired
    private SysAreaService areaService;

    @Autowired
    AreaService AreaService;

    @Autowired
    private SysDeptService deptService;

    @Autowired
    private SysAreaDeptService areaDeptService;

    /**
     * 首页表格查询
     *
     * @param search
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public JsonResult getAreaListData(@RequestParam(value = "search", required = false, defaultValue = "") String search, Page page) {
        return areaService.getTableDataList(page, search);
    }

    /**
     * 获取所有的区域数据 上下级关系的
     *
     * @return
     */
    @RequestMapping("/getAreaTreeData")
    public JsonResult getAreaTreeData() {
        return areaService.getAreaTreeList();
    }

    /**
     * 通过归属区域的编号查询归属区域的信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getAreaDataById")
    public JsonResult getAreaDataById(String id) {
        if (StringUtils.isBlank(id)) {
            return JsonResult.errorForEmpty();
        }
        return areaService.getAreaDataById(id);
    }

    @SysLog("归属区域更新操作")
    @PostMapping("/update")
    public JsonResult updateAreaById(@RequestBody Map<String, Object> params) {
        SysAreaEntity entity = JSONObject.parseObject(JSONObject.toJSONString(params), SysAreaEntity.class);
        if (entity == null || entity.getAreaId() == null) {
            return JsonResult.errorForEmpty();
        }
        entity.setUpdtm(new Date());
        boolean state = areaService.updateById(entity);
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

    @SysLog("归属区域新增操作")
    @PostMapping("/save")
    public JsonResult saveArea(@RequestBody Map<String, Object> params) {
        SysAreaEntity entity = JSONObject.parseObject(JSONObject.toJSONString(params), SysAreaEntity.class);
        if (entity == null || StringUtils.isBlank(entity.getName())) {
            return JsonResult.errorForEmpty();
        }
        entity.setAreaId(null);
        entity.setInstm(new Date());
        boolean state = areaService.insert(entity);
        if (state == false) {
            return JsonResult.actionSuccess();
        }
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

    @SysLog("归属区域删除操作")
    @PostMapping("/delete")
    public JsonResult deleteAreaById(@RequestBody List<String> id) {
        if (id == null || id.isEmpty()) {
            return JsonResult.errorForEmpty();
        }
        boolean state = areaService.deleteBatchIds(id);
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

    /**
     * 功能描述: 
     * 获取签约公司
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/16 9:52
     */
    @GetMapping("/getAreaList")
    public JsonResult getAreaList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
//        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"AreaId"})){
//            result.putFailed("AreaId不能为空！");
//            return  result;
//        }
        params.put("type",1);
        result.put(AreaService.getAreaList(params));
        return result;
    }

    /**
     * 功能描述: 
     * 获取所有的公司列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/16 14:12
     */
    @GetMapping("/getAllAreaList")
    public JsonResult getAllAreaList(){
        JsonResult result=new JsonResult();
        return result.put(AreaService.getAllAreaList());
    }

    /**
     * 获取服务中心编号集合 通过公司编号
     *
     * @param areaId
     * @return
     */
    @PostMapping("/getCenterServiceIdList")
    public List<String> getCenterServiceIdList(Integer areaId) {
        List<String> data = Lists.newArrayList();
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("area_id", areaId);
        List<Map<String, Object>> list = areaDeptService.selectMaps(wrapper);
        for (Map<String, Object> entity : list) {
            wrapper = new EntityWrapper();
            wrapper.eq("dept_id", MapUtils.getString(entity, "deptId"));
            wrapper.eq("`type`", 3);
            SysDeptEntity one = deptService.selectOne(wrapper);
            if (one == null) {
                wrapper = new EntityWrapper();
                wrapper.eq("area_id", MapUtils.getString(entity, "areaId"));
                wrapper.eq("dept_id", MapUtils.getString(entity, "deptId"));
                areaDeptService.delete(wrapper);
                continue;
            }
            data.add(one.getDeptId().toString());
        }
        return data;
    }
}
